package truedano.truedanostock;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends Util {
	public Button add_stock_btn,list_all_stock_btn,delete_all_stock,continue_add,backup_restore,setting;
	public TextView version_name;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		createDirIfNotExists(getApplicationName(getApplicationContext()));
	}
	
	@Override
	protected void onResume() {
		setView();
		super.onResume();
	}
	
	private void setView(){
		add_stock_btn = (Button)findViewById(R.id.add_stock_btn);
		add_stock_btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(isInternetExist() == false){
					showToast(getString(R.string.no_internet));
					return;
				}
				setStockNum();
			}
			
		});
		list_all_stock_btn = (Button)findViewById(R.id.list_all_stock_btn);
		list_all_stock_btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				gotoActivity(ACTIVITY_LISTALLSTOCK2);
			}
			
		});
		
		delete_all_stock = (Button)findViewById(R.id.delete_all_stock);
		delete_all_stock.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				deleteAllStock();
			}
			
		});
		
		version_name = (TextView)findViewById(R.id.version_name);
		version_name.setText(getVersionName());
		setButtonView();
		
		continue_add = (Button)findViewById(R.id.continue_add);
		continue_add.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(isInternetExist() == false){
					showToast(getString(R.string.no_internet));
					return;
				}
				gotoActivity(ACTIVITY_CONTINUE_ADD_STOCK);
			}
			
		});
		
		backup_restore = (Button)findViewById(R.id.backup_restore);
		backup_restore.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				setBackupRestore();
			}
			
		});
		
		setting = (Button)findViewById(R.id.setting);
		setting.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				gotoActivity(ACTIVITY_SETTING);
			}
			
		});
	}
	
	private void setButtonView(){
		int DBCount = getDBCount();
		if(DBCount == 0){
			list_all_stock_btn.setEnabled(false);
			delete_all_stock.setEnabled(false);
		}
		else{
			list_all_stock_btn.setEnabled(true);
			delete_all_stock.setEnabled(true);
		}
	}
	
	private void enableAllButton(boolean enable){
		if(enable == true){
			list_all_stock_btn.setEnabled(true);
			list_all_stock_btn.setBackgroundResource(R.color.blue);
			delete_all_stock.setEnabled(true);
			delete_all_stock.setBackgroundResource(R.color.blue);
		}
		else{
			list_all_stock_btn.setEnabled(false);
			list_all_stock_btn.setBackgroundResource(R.color.red);
			delete_all_stock.setEnabled(false);
			delete_all_stock.setBackgroundResource(R.color.red);
		}
	}
	
	private void deleteAllStock(){
		new AlertDialog.Builder(this).setMessage(R.string.delete_it)
		.setNeutralButton(R.string.no, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		})
		.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				deleteAllDB();
				setButtonView();
			}
		}).show();
	}
	
	private void setBackupRestore(){
		new AlertDialog.Builder(this).setMessage(R.string.backup_restore)
		.setNeutralButton(R.string.restore, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				restoreFile2DB(getApplicationContext());
				setHandler(H_SET_BUTTON_VIEW,handler);
				dialog.cancel();
			}
		})
		.setPositiveButton(R.string.backup, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				backupDB2File(getApplicationContext());
				setHandler(H_SET_BUTTON_VIEW,handler);
				dialog.cancel();
			}
		}).show();
	}
	
	private void setStockNum(){
		View myview = LayoutInflater.from(this).inflate(R.layout.add_stock_dialog, null);
		final EditText add_stock_et = (EditText)myview.findViewById(R.id.add_stock_et);
		new AlertDialog.Builder(this).setTitle(R.string.add_stock)
		.setView(myview)
		.setNeutralButton(R.string.no,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,int which) 
					{
						dialog.cancel();
					}
		})
		.setPositiveButton(R.string.yes,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,int which) 
					{
						String num = add_stock_et.getText().toString();
						if(num.equals("") || num.length()<4){
							dialog.cancel();
							showToast(getString(R.string.stock_number_err));
							return;
						}
						if(isStockNumExist(num) == true){
							dialog.cancel();
							showToast(getString(R.string.stock_number_exist));
							return;
						}
						//get data from web site////////////////////////////////////////////////////////
						final String numTmp = num;
						Thread getData = new Thread(){
							@Override
							public void run() {
								setHandler(H_DISABLE_ALL_BUTTIN,handler);
								String result = getHtmlResult(numTmp);
								String current_price = getCurrentPrice(result,numTmp);
								if(current_price.equals("")){
									setHandler(H_STOCK_NUMBER_ERROR,handler);
								}
								else{
									String stock_name = getStockName(result,numTmp);
									float div  = getCashDividend(numTmp);
									addDB(MyDB.STOCK_NUMBER,numTmp);
									writeAllDBItems(numTmp,current_price,stock_name,div);
								}
								setHandler(H_ADD_SUCCESS,handler);
							}
						};
						getData.start();
					}
				})
				.show();
	}
	
	public static final int H_ADD_SUCCESS     = 0;
	public static final int H_DISABLE_ALL_BUTTIN  = 1;
	public static final int H_STOCK_NUMBER_ERROR = 2;
	public static final int H_SET_BUTTON_VIEW = 3;
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what){
			case H_ADD_SUCCESS:
				setButtonView();
				enableAllButton(true);
				showToast(getString(R.string.add_stock));
				break;
			case H_DISABLE_ALL_BUTTIN:
				enableAllButton(false);
				break;
			case H_STOCK_NUMBER_ERROR:
				showToast(getString(R.string.stock_number_err));
				break;
			case H_SET_BUTTON_VIEW:
				setButtonView();
				break;
			}
		}
	};
	
}
